/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver.server;

import java.io.Serializable;
import java.util.Date;
import jeeobserver.server.Statistics.DateStatistics;
import jeeobserver.server.Statistics.NumberStatistics;
import jeeobserver.server.Statistics.SamplingStatistics;

/**
 * The Class HttpSessionStatistics.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public abstract class HttpSessionStatistics extends SamplingStatistics implements Serializable {

	 private static final long serialVersionUID = 40L;

	 private final DateStatistics dateStatistics;

	 private final NumberStatistics sessionsCreatedStatistics;

	 private final NumberStatistics sessionsDestroyedStatistics;

	 private final NumberRangeStatistics sessionsDestroyedTimeNanosStatistics;

	 public HttpSessionStatistics(Date date, int count, DateStatistics dateStatistics, NumberStatistics sessionsCreatedStatistics, NumberStatistics sessionsDestroyedStatistics, NumberRangeStatistics sessionsDestroyedTimeNanosStatistics) {
			super(date, count);
			this.dateStatistics = dateStatistics;
			this.sessionsCreatedStatistics = sessionsCreatedStatistics;
			this.sessionsDestroyedStatistics = sessionsDestroyedStatistics;
			this.sessionsDestroyedTimeNanosStatistics = sessionsDestroyedTimeNanosStatistics;
	 }

	 protected HttpSessionStatistics(Date date, HttpSessionStatistics statistics) {
			this(date, statistics.getCount(),
							statistics.getDateStatistics(),
							statistics.getSessionsCreatedStatistics(),
							statistics.getSessionsDestroyedStatistics(),
							statistics.getSessionsDestroyedTimeNanosStatistics());
	 }

	 public void add(HttpSessionStatistics statistics) {

			super.add(statistics.getCount());

			this.dateStatistics.add(statistics.getDateStatistics());

			this.sessionsCreatedStatistics.add(statistics.getSessionsCreatedStatistics());
			this.sessionsDestroyedStatistics.add(statistics.getSessionsDestroyedStatistics());
			this.sessionsDestroyedTimeNanosStatistics.add(statistics.getSessionsDestroyedTimeNanosStatistics());
	 }

	 public DateStatistics getDateStatistics() {
			return dateStatistics;
	 }

	 public NumberStatistics getSessionsCreatedStatistics() {
			return sessionsCreatedStatistics;
	 }

	 public NumberStatistics getSessionsDestroyedStatistics() {
			return sessionsDestroyedStatistics;
	 }

	 public NumberRangeStatistics getSessionsDestroyedTimeNanosStatistics() {
			return sessionsDestroyedTimeNanosStatistics;
	 }

	 public static final class TimeStatistics extends HttpSessionStatistics {

			private static final long serialVersionUID = 40L;

			public TimeStatistics(Date date, HttpSessionStatistics statistics) {
				 super(date, statistics);
			}
	 }

	 public static final class TotalStatistics extends HttpSessionStatistics {

			private static final long serialVersionUID = 40L;

			private final int samplingPeriod;

			private final String name;

			private final NumberTrend sessionsCreatedTrend;

			private final NumberTrend sessionsDestroyedTrend;

			private final NumberTrend sessionsDestroyedTimeNanosTrend;

			public TotalStatistics(HttpSessionStatistics statistics, String name, int samplingPeriod) {
				 super(statistics.getDate(), statistics);

				 this.samplingPeriod = samplingPeriod;

				 this.name = name;

				 this.sessionsCreatedTrend = new NumberTrend();
				 this.sessionsCreatedTrend.add(statistics.getDate(), statistics.getSessionsCreatedStatistics().getSum(), 1);

				 this.sessionsDestroyedTrend = new NumberTrend();
				 this.sessionsDestroyedTrend.add(statistics.getDate(), statistics.getSessionsDestroyedStatistics().getSum(), 1);

				 this.sessionsDestroyedTimeNanosTrend = new NumberTrend();
				 this.sessionsDestroyedTimeNanosTrend.add(statistics.getDate(), statistics.getSessionsDestroyedTimeNanosStatistics().getSum(), (int) statistics.getSessionsDestroyedStatistics().getSum());
			}

			@Override
			public void add(HttpSessionStatistics statistics) {
				 super.add(statistics);

				 this.sessionsCreatedTrend.add(statistics.getDate(), statistics.getSessionsCreatedStatistics().getSum(), 1);
				 this.sessionsDestroyedTrend.add(statistics.getDate(), statistics.getSessionsDestroyedStatistics().getSum(), 1);
				 this.sessionsDestroyedTimeNanosTrend.add(statistics.getDate(), statistics.getSessionsDestroyedTimeNanosStatistics().getSum(), (int) statistics.getSessionsDestroyedStatistics().getSum());
			}

			public void calculateTrendStatistics() {
				 this.sessionsCreatedTrend.calculateTrendStatistics();
				 this.sessionsDestroyedTrend.calculateTrendStatistics();
				 this.sessionsDestroyedTimeNanosTrend.calculateTrendStatistics();
			}

			public int getSamplingPeriod() {
				 return samplingPeriod;
			}

			public String getName() {
				 return name;
			}

			public NumberTrend getSessionsCreatedTrend() {
				 return sessionsCreatedTrend;
			}

			public NumberTrend getSessionsDestroyedTrend() {
				 return sessionsDestroyedTrend;
			}

			public NumberTrend getSessionsDestroyedTimeNanosTrend() {
				 return sessionsDestroyedTimeNanosTrend;
			}
	 }

	 public static class Element extends Statistics.Element {

			private static final long serialVersionUID = 40L;

			private String context;

			private String project;

			public Element(int id, String context, String project) {
				 super(id);
				 this.context = context;
				 this.project = project;
			}

			public Element(String context, String project) {
				 super((context + "|" + project).hashCode());
				 this.context = context;
				 this.project = project;
			}

			/**
			 * Gets the project.
			 *
			 * @return the project
			 */
			public String getProject() {
				 return this.project;
			}

			/**
			 * Gets the context.
			 *
			 * @return the context
			 */
			public String getContext() {
				 return this.context;
			}
	 }
}
